package top.drinyeit.nongmao.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import top.drinyeit.nongmao.R;
import top.drinyeit.nongmao.base.BaseActivity;
import top.drinyeit.nongmao.dialog.PayDialog;
import top.drinyeit.nongmao.model.RechargeBean;

/**
 * Created by wu on 2018/1/6.
 */

public class RechargeNewActivity extends BaseActivity {

    private EditText number, password, et_recharge_amount;
    private TextView recharge, tv_youhui;
    private ImageView delete_all1, delete_all0;

    //    private RechargePresenter rechargePresenter;
    double youHui, price = 0;
    RechargeBean bean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_recharge_new);

        number = findViewById(R.id.number);
        password = findViewById(R.id.password);
        recharge = findViewById(R.id.recharge);
        delete_all1 = findViewById(R.id.delete_all1);
        delete_all0 = findViewById(R.id.delete_all0);
        et_recharge_amount = findViewById(R.id.et_recharge_amount);
        tv_youhui = findViewById(R.id.tv_youhui);
        initData();
        init();

        setTitle(true, "充值");
    }

    public void initData() {
        bean = (RechargeBean) getIntent().getSerializableExtra("bean");
        price = bean.getPrice();
        youHui = bean.getYouHui();
        et_recharge_amount.setText(price + "");
        tv_youhui.setText("优惠金额" + youHui);
//        rechargePresenter = new RechargePresenter(this);
    }

    @Override
    public void init() {
        recharge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (et_recharge_amount.getText().length() == 0) {
                    showToastMsg("请输入充值金额");
                    return;
                }
                final PayDialog payDialog = new PayDialog(RechargeNewActivity.this, 0.01,
                        "1");
                payDialog.show();
                payDialog.isSupportWYuE(false);
                payDialog.order_price.setText("¥ " + 0.01);
                payDialog.setCancelable(false);
                payDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialog) {
                        payDialog.dismiss();
                    }
                });
//                rechargePresenter.recharge(number.getText().toString(), password.getText().toString());
            }
        });

        number.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    delete_all0.setVisibility(View.VISIBLE);
                } else {
                    delete_all0.setVisibility(View.GONE);
                }
            }
        });

        password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    delete_all1.setVisibility(View.VISIBLE);
                } else {
                    delete_all1.setVisibility(View.GONE);
                }
            }
        });

        delete_all0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                number.setText("");
            }
        });
        delete_all1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                password.setText("");
            }
        });
    }
}
